/*
 * cmd_efuse_simple_test.c
 *
 *  Created on: Nov 10, 2016
 *      Author: duobao
 */


/*
 * cmd_efuse_test.c
 *
 *  Created on: Sep 28, 2016
 *      Author: duobao
 */
#ifdef CONFIG_FH_AES_V2
#include "../drivers/crypto/fh_aes_v2.h"
#else
#include "../drivers/crypto/fh_aes.h"
#endif
#include <common.h>
#include <command.h>


#define EFUSE_CMD_PARA_MAX		8
#define EFUSE_ADDR_PARA_MAX		63
#define EFUSE_WVALUE_PARA_MAX	255
#define EFUSE_AESKEYID_PARA_MAX	7

#ifdef CONFIG_FH_AES_V2

int efuse_cmd(cmd_tbl_t *cmdtp,int flag,int argc,char *argv[])
{
	unsigned int cmd ,addr,wvalue,aeskeyid,wvalue_data;
	char *endp;
	int ret;
	EFUSE_INFO_V2 efuse_info;

	cmd = 0;
	addr = 0;
	wvalue = 0;

	cmd = simple_strtoul(argv[1], &endp, 0);
	addr = simple_strtoul(argv[2], &endp, 0);
	wvalue = simple_strtoul(argv[3], &endp, 16);
	aeskeyid = simple_strtoul(argv[4], &endp, 0);
	wvalue_data = simple_strtoul(argv[5], &endp, 16);

	memset(&efuse_info, 0, sizeof(EFUSE_INFO_V2));
	if (argc == 5)
		efuse_info.status.error = EFUSE_DEBUG_WRITE_ENTRY; 
	else if (argc == 6)
		efuse_info.status.error = 0; 
	else {
		cmd_usage(cmdtp);
		return -1;
	}

	efuse_info.cmd = cmd;
	efuse_info.addr = addr;
	efuse_info.wvalue = wvalue;
	efuse_info.aeskeyid = aeskeyid;
	efuse_info.wvalue_data = wvalue_data;
	fh_efuse_ioctl(&efuse_info, IOCTL_EFUSE_DEBUG, 0);
 
	return 0;
}

U_BOOT_CMD(
	efuse_cmd,6,1,efuse_cmd,"usage:efuse_debug, efuse debug\n","upgrage:null \n"
);

int efuse_dump(cmd_tbl_t *cmdtp,int flag,int argc,char *argv[])
{
	EFUSE_INFO_V2 EFUSE_INFO_V2_obj;
	EFUSE_INFO_V2 * EFUSE_INFO_V2 = &EFUSE_INFO_V2_obj;

	fh_efuse_ioctl( EFUSE_INFO_V2, IOCTL_EFUSE_DUMP_REGISTER, 0);
	return 0;
}

U_BOOT_CMD(
	efuse_dump,6,1,efuse_dump,"usage:efuse_debug, efuse debug\n","upgrage:null \n"
);

#else
static int efuse_cmd_para_check(EFUSE_INFO * efuse_info){

	unsigned int cmd ,addr,wvalue,aeskeyid;
	cmd = efuse_info->cmd;
	addr = efuse_info->addr;
	wvalue = efuse_info->wvalue;
	aeskeyid = efuse_info->aeskeyid;
	/* check cmd */
	if (cmd > EFUSE_CMD_PARA_MAX){
		printf("cmd %d out of range %d\n", cmd, EFUSE_CMD_PARA_MAX);
		return -1;
	}
	/* check addr */
	if (addr > EFUSE_ADDR_PARA_MAX){
		printf("addr %d out of range %d\n", addr, EFUSE_ADDR_PARA_MAX);
		return -1;
	}
	/* check wvalue */
	if (wvalue > EFUSE_WVALUE_PARA_MAX){
		printf("wvalue %d out of range %d\n", wvalue, EFUSE_WVALUE_PARA_MAX);
		return -1;
	}
	/* check aeskeyid */
	if (aeskeyid > EFUSE_AESKEYID_PARA_MAX){
		printf("aeskeyid %d out of range %d\n", aeskeyid, EFUSE_AESKEYID_PARA_MAX);
		return -1;
	}
	/* check lock */
	if ((cmd = 2) && (addr == 63)){
		if(wvalue & (1<<3)){
			printf("please DO NOT lock efuse area 3\n");
			return -1;
		}
	}
	return 0;
}

int efuse_cmd(cmd_tbl_t *cmdtp,int flag,int argc,char *argv[])
{
	unsigned int cmd ,addr,wvalue,aeskeyid;
	char *endp;
	int ret;
	EFUSE_INFO efuse_info_obj;
	EFUSE_INFO * efuse_info = &efuse_info_obj;
	cmd = 0;
	addr = 0;
	wvalue = 0;
	aeskeyid = 0;

	cmd = simple_strtoul(argv[1], &endp, 0);
	addr = simple_strtoul(argv[2], &endp, 0);
	wvalue = simple_strtoul(argv[3], &endp, 0);
	aeskeyid = simple_strtoul(argv[4], &endp, 0);
	efuse_info->cmd = cmd;
	efuse_info->addr = addr;
	efuse_info->wvalue = wvalue;
	efuse_info->aeskeyid = aeskeyid;
	ret = efuse_cmd_para_check(efuse_info);
	if(ret){
		return -1;
	}
	fh_efuse_ioctl( efuse_info, IOCTL_EFUSE_DEBUG, 0);
	return 0;
}

U_BOOT_CMD(

		efuse_cmd,5,1,efuse_cmd,"usage:efuse_debug, efuse debug\n","upgrage:null \n"

);


int efuse_dump(cmd_tbl_t *cmdtp,int flag,int argc,char *argv[])
{
	EFUSE_INFO efuse_info_obj;
	EFUSE_INFO * efuse_info = &efuse_info_obj;

	fh_efuse_ioctl( efuse_info, IOCTL_EFUSE_DUMP_REGISTER, 0);

	return 0;

}

U_BOOT_CMD(

		efuse_dump,5,1,efuse_dump,"usage:efuse_debug, efuse debug\n","upgrage:null \n"

);

#endif
